let express = require('express');
let router = express.Router();

const wordService = require('../service/wordService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

// 获取 20个 待背诵单词
router.get('/getAnyWord', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await wordService.getAnyWord(uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 记录背诵进度
router.post('/saveUserProgress', async function(req, res, next) {
    let time = req.time;
    let uid = req.jwtResult.data.uid;
    let { progress } = req.body;
    if (!Array.isArray(progress) || progress.length == 0) {
        return res.status(400).json({ success: false, msg: 'progress为空' });
    }
    let result = await wordService.saveUserProgress(uid, progress, time);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取用户背诵进度
router.get('/getUserProgress', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await wordService.getUserProgress(uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取这个单词的信息
router.post('/loadThisWord', async function(req, res, next) {
    let wordId = req.body.wordId;
    let result = await wordService.loadThisWord(wordId);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
